package com.shareit.ocr.domain.template;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**  
* 类说明   
*  
* @author geai
* @date 2018年1月22日  新建  
*/
public class InvoiceData04ItemTemplate extends InvoiceDataItemTemplate {
	
	private String rule = "○☺5>4☺878☺-25☺▽";
	
	public InvoiceData04ItemTemplate() {
		super();
		InvoiceDataField field;
		field = new InvoiceDataField("xmmc");
		this.fields.add(field);
		field = new InvoiceDataField("xh");
		this.fields.add(field);
		field = new InvoiceDataField("dw");
		this.fields.add(field);
		field = new InvoiceDataField("sl");
		this.fields.add(field);
		field = new InvoiceDataField("dj");
		this.fields.add(field);
		field = new InvoiceDataField("je");
		this.fields.add(field);
		field = new InvoiceDataField("rate");
		this.fields.add(field);
		field = new InvoiceDataField("se");
		this.fields.add(field);
	}

	public void setDataFields(Object obj) {
		
		//WEB
		if(obj instanceof WebElement) {
			WebElement element = (WebElement)obj;
			this.setFieldValue("xmmc", element.findElements(By.xpath(".//td[1]/span")).get(0).getText());
			this.setFieldValue("xh",element.findElements(By.xpath(".//td[2]/span")).get(0).getText());
			this.setFieldValue("dw",element.findElements(By.xpath(".//td[3]/span")).get(0).getText());
			this.setFieldValue("sl",element.findElements(By.xpath(".//td[4]/span")).get(0).getText());
			this.setFieldValue("dj",element.findElements(By.xpath(".//td[5]/span")).get(0).getText());
			this.setFieldValue("je",element.findElements(By.xpath(".//td[6]/span")).get(0).getText());
			this.setFieldValue("rate",element.findElements(By.xpath(".//td[7]/span")).get(0).getText());
			this.setFieldValue("se",element.findElements(By.xpath(".//td[8]/span")).get(0).getText());

		}else{
			//API
			String row = (String)obj;
			String [] cols = row.split("█");
			this.setFieldValue("xmmc",cols[0].replace(rule.split("☺")[4], ""));
			this.setFieldValue("xh",cols[1]);
			this.setFieldValue("dw",cols[2]);
			this.setFieldValue("sl",cols[3]);
			this.setFieldValue("dj",cols[4]);
			this.setFieldValue("je",cols[5]);
			this.setFieldValue("rate","".equals(cols[6].trim())?"":cols[6] + "%");
			this.setFieldValue("se",cols[7]);
			}
		}
	
	@Override
	public String getSplitChar() {
		return rule.split("☺")[0];
	}
	
	@Override
	public Object copy(){
		InvoiceData04ItemTemplate template = new InvoiceData04ItemTemplate();
		for(InvoiceDataField f:this.fields) {
			template.setFieldValue(f.getName(), f.getValue());
		}
		return template;
	}
}
